/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
* ComGLHoodIOptions.java
*
* Created on December 7, 2007, 10:12 PM
 */
package gui.genomemap.view.inputView.options;

import genomemap.data.RFLPImputation;

import gui.genomemap.inputModel.ComGLHoodIModel;

import javautil.swing.SwingUtil;

/**
 *
 * @author  stewari1
 */
public class ComGLHoodIOptions extends javax.swing.JDialog {

    /** Field description */
    private double imputationProb = 0.5;


    // Variables declaration - do not modify//GEN-BEGIN:variables

    /** Field description */
    private javax.swing.ButtonGroup applyImputation_JButtonGroup;

    /** Field description */
    private javax.swing.JCheckBox computingSE_JCheckBox;

    /** Field description */
    private javax.swing.JTextField convEpsilon_JTextField;

    /** Field description */
    private javax.swing.JPanel general_JPanel;

    /** Field description */
    private javax.swing.ButtonGroup imputationOptions_ButtonGroup;

    /** Field description */
    private javax.swing.JPanel imputationOptions_JPanel1;

    /** Field description */
    private javax.swing.JRadioButton imputePolicyProb_JRadioButton;

    /** Field description */
    private javax.swing.JTextField imputeProb_JTextField;

    /** Field description */
    private javax.swing.JRadioButton imputing_JRadioButton;

    /** Field description */
    private javax.swing.JTextField iteration_JTextField;

    /** Field description */
    private javax.swing.JLabel jLabel1;

    /** Field description */
    private javax.swing.JLabel jLabel2;

    /** Field description */
    private javax.swing.JPanel jPanel1;

    /** Field description */
    private javax.swing.JPanel jPanel2;

    /** Field description */
    private javax.swing.JPanel missingVal_JPanel1;

    /** Field description */
    private javax.swing.JCheckBox pValue_JCheckBox;

    /** Field description */
    private javax.swing.JRadioButton removeSpore_JRadioButton;

    /** Creates new form ComGLHoodIOptions */
    public ComGLHoodIOptions(java.awt.Frame parent, boolean modal) {

        super(parent, modal);

        initComponents();
    }

    /**
     * This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */


    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        applyImputation_JButtonGroup  = new javax.swing.ButtonGroup();
        imputationOptions_ButtonGroup = new javax.swing.ButtonGroup();
        general_JPanel                = new javax.swing.JPanel();
        jPanel1                       = new javax.swing.JPanel();
        jLabel1                       = new javax.swing.JLabel();
        iteration_JTextField          = new javax.swing.JTextField();
        jLabel2                       = new javax.swing.JLabel();
        convEpsilon_JTextField        = new javax.swing.JTextField();
        missingVal_JPanel1            = new javax.swing.JPanel();
        removeSpore_JRadioButton      = new javax.swing.JRadioButton();
        imputing_JRadioButton         = new javax.swing.JRadioButton();
        imputationOptions_JPanel1     = new javax.swing.JPanel();
        imputePolicyProb_JRadioButton = new javax.swing.JRadioButton();
        imputeProb_JTextField         = new javax.swing.JTextField();
        jPanel2                       = new javax.swing.JPanel();
        computingSE_JCheckBox         = new javax.swing.JCheckBox();
        pValue_JCheckBox              = new javax.swing.JCheckBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Advanced Input Settings");
        setAlwaysOnTop(true);
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setModal(true);
        addWindowListener(new java.awt.event.WindowAdapter() {

            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }

        });
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("LHood Settings"));
        jLabel1.setText("Iteration Limit");
        iteration_JTextField.setText("150");
        jLabel2.setText("Convergence EPSILON");
        convEpsilon_JTextField.setText("0.0001");
        missingVal_JPanel1.setBorder(
            javax.swing.BorderFactory.createTitledBorder("Missing Value Options"));
        applyImputation_JButtonGroup.add(removeSpore_JRadioButton);
        removeSpore_JRadioButton.setText("Remove Spores with Missing Values");
        removeSpore_JRadioButton.setToolTipText(
            "<html>\nSpore  is only removed if the missing vaue gene is \n<br/>present in the gene order under computation.\n</html>");
        removeSpore_JRadioButton.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        removeSpore_JRadioButton.setMargin(new java.awt.Insets(0, 0, 0, 0));
        applyImputation_JButtonGroup.add(imputing_JRadioButton);
        imputing_JRadioButton.setSelected(true);
        imputing_JRadioButton.setText(
            "Apply following strategies for imputation on the Missing Values");
        imputing_JRadioButton.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        imputing_JRadioButton.setMargin(new java.awt.Insets(0, 0, 0, 0));
        imputing_JRadioButton.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                imputing_JRadioButtonActionPerformed(evt);
            }

        });
        imputationOptions_JPanel1.setBorder(
            javax.swing.BorderFactory.createTitledBorder("Imputation Strategies"));
        imputationOptions_ButtonGroup.add(imputePolicyProb_JRadioButton);
        imputePolicyProb_JRadioButton.setSelected(true);
        imputePolicyProb_JRadioButton.setText("Apply probability for 0<->1 case with prob: ");
        imputePolicyProb_JRadioButton.setToolTipText(
            "<html>\n0<->0 means the missing value gene on both sides has 0.<br>\nIf both side values are the same then that value is used for imputation.<br>\nWhen we have opposing values 0<->1 , 0 is chosen with the specified prob<br>\nor else 1 gets chosen.\n</html>");
        imputePolicyProb_JRadioButton.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0,
                0, 0));
        imputePolicyProb_JRadioButton.setMargin(new java.awt.Insets(0, 0, 0, 0));
        imputeProb_JTextField.setText("0.5");

        javax.swing.GroupLayout imputationOptions_JPanel1Layout =
            new javax.swing.GroupLayout(imputationOptions_JPanel1);

        imputationOptions_JPanel1.setLayout(imputationOptions_JPanel1Layout);
        imputationOptions_JPanel1Layout.setHorizontalGroup(
            imputationOptions_JPanel1Layout.createParallelGroup(
                javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                imputationOptions_JPanel1Layout.createSequentialGroup().addComponent(
                    imputePolicyProb_JRadioButton).addPreferredGap(
                    javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(
                    imputeProb_JTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 35,
                    javax.swing.GroupLayout.PREFERRED_SIZE).addContainerGap(48, Short.MAX_VALUE)));
        imputationOptions_JPanel1Layout.setVerticalGroup(
            imputationOptions_JPanel1Layout.createParallelGroup(
                javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                imputationOptions_JPanel1Layout.createSequentialGroup().addGroup(
                    imputationOptions_JPanel1Layout.createParallelGroup(
                        javax.swing.GroupLayout.Alignment.BASELINE).addComponent(
                        imputePolicyProb_JRadioButton).addComponent(
                        imputeProb_JTextField, javax.swing.GroupLayout.PREFERRED_SIZE,
                        javax.swing.GroupLayout.DEFAULT_SIZE,
                        javax.swing.GroupLayout.PREFERRED_SIZE)).addContainerGap(
                            127, Short.MAX_VALUE)));

        javax.swing.GroupLayout missingVal_JPanel1Layout =
            new javax.swing.GroupLayout(missingVal_JPanel1);

        missingVal_JPanel1.setLayout(missingVal_JPanel1Layout);
        missingVal_JPanel1Layout.setHorizontalGroup(
            missingVal_JPanel1Layout.createParallelGroup(
                javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                missingVal_JPanel1Layout.createSequentialGroup().addContainerGap().addGroup(
                    missingVal_JPanel1Layout.createParallelGroup(
                        javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                        missingVal_JPanel1Layout.createParallelGroup(
                            javax.swing.GroupLayout.Alignment.TRAILING, false).addComponent(
                            removeSpore_JRadioButton,
                            javax.swing.GroupLayout.Alignment.LEADING).addComponent(
                                imputing_JRadioButton, javax.swing.GroupLayout.Alignment.LEADING,
                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                Short.MAX_VALUE)).addComponent(
                                    imputationOptions_JPanel1,
                                    javax.swing.GroupLayout.DEFAULT_SIZE,
                                    javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)).addGap(
                                        101, 101, 101)));
        missingVal_JPanel1Layout.setVerticalGroup(
            missingVal_JPanel1Layout.createParallelGroup(
                javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                missingVal_JPanel1Layout.createSequentialGroup().addContainerGap().addComponent(
                    removeSpore_JRadioButton).addPreferredGap(
                    javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(
                    imputing_JRadioButton).addPreferredGap(
                    javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(
                    imputationOptions_JPanel1, javax.swing.GroupLayout.DEFAULT_SIZE,
                    javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addContainerGap()));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);

        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout
            .setHorizontalGroup(jPanel1Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup().addGap(10, 10, 10)
                    .addGroup(jPanel1Layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel1).addComponent(jLabel2)).addGap(17, 17, 17)
                            .addGroup(jPanel1Layout
                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(convEpsilon_JTextField,
                                    javax.swing.GroupLayout.PREFERRED_SIZE, 66,
                                    javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(iteration_JTextField,
                                            javax.swing.GroupLayout.PREFERRED_SIZE, 39,
                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                                                    .addGap(324, 324, 324))
                                                        .addGroup(jPanel1Layout
                                                            .createSequentialGroup()
                                                                .addContainerGap()
                                                                    .addComponent(missingVal_JPanel1,
                                                                        javax.swing.GroupLayout
                                                                            .DEFAULT_SIZE, javax
                                                                                .swing.GroupLayout
                                                                                    .DEFAULT_SIZE, Short
                                                                                        .MAX_VALUE)
                                                                                            .addGap(55,
                                                                                                55,
                                                                                                55)));
        jPanel1Layout
            .setVerticalGroup(jPanel1Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap()
                    .addGroup(jPanel1Layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1)
                        .addComponent(iteration_JTextField, javax.swing.GroupLayout
                            .PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing
                            .GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout
                                    .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel2)
                                    .addComponent(convEpsilon_JTextField, javax.swing.GroupLayout
                                        .PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax
                                        .swing.GroupLayout.PREFERRED_SIZE))
                                            .addPreferredGap(javax.swing.LayoutStyle
                                                .ComponentPlacement.RELATED)
                                                    .addComponent(missingVal_JPanel1, javax.swing
                                                        .GroupLayout.PREFERRED_SIZE, javax.swing
                                                            .GroupLayout.DEFAULT_SIZE, javax.swing
                                                                .GroupLayout.PREFERRED_SIZE)
                                                                    .addContainerGap(javax.swing
                                                                        .GroupLayout
                                                                            .DEFAULT_SIZE, Short
                                                                                .MAX_VALUE)));
        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Optional Operations"));
        computingSE_JCheckBox.setText("Compute Standard Errors");
        computingSE_JCheckBox.setToolTipText("The standard deviation of the parameter estimates.");
        computingSE_JCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        computingSE_JCheckBox.setMargin(new java.awt.Insets(0, 0, 0, 0));
        computingSE_JCheckBox.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                computingSE_JCheckBoxActionPerformed(evt);
            }

        });
        pValue_JCheckBox.setSelected(true);
        pValue_JCheckBox.setText("Compute P-Value");
        pValue_JCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        pValue_JCheckBox.setMargin(new java.awt.Insets(0, 0, 0, 0));

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);

        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                jPanel2Layout.createSequentialGroup().addGroup(
                    jPanel2Layout.createParallelGroup(
                        javax.swing.GroupLayout.Alignment.LEADING).addComponent(
                        computingSE_JCheckBox).addComponent(pValue_JCheckBox)).addContainerGap(
                            379, Short.MAX_VALUE)));
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                jPanel2Layout.createSequentialGroup().addComponent(
                    computingSE_JCheckBox).addPreferredGap(
                    javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(
                    pValue_JCheckBox).addContainerGap(38, Short.MAX_VALUE)));

        javax.swing.GroupLayout general_JPanelLayout = new javax.swing.GroupLayout(general_JPanel);

        general_JPanel.setLayout(general_JPanelLayout);
        general_JPanelLayout.setHorizontalGroup(
            general_JPanelLayout.createParallelGroup(
                javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                general_JPanelLayout.createSequentialGroup().addContainerGap().addGroup(
                    general_JPanelLayout.createParallelGroup(
                        javax.swing.GroupLayout.Alignment.LEADING).addComponent(
                        jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE,
                        javax.swing.GroupLayout.DEFAULT_SIZE,
                        javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(
                            jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE,
                            javax.swing.GroupLayout.DEFAULT_SIZE,
                            Short.MAX_VALUE)).addContainerGap()));
        general_JPanelLayout.setVerticalGroup(
            general_JPanelLayout.createParallelGroup(
                javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                general_JPanelLayout.createSequentialGroup().addContainerGap().addComponent(
                    jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
                    javax.swing.GroupLayout.DEFAULT_SIZE,
                    javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(
                        javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(
                        jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE,
                        javax.swing.GroupLayout.DEFAULT_SIZE,
                        javax.swing.GroupLayout.PREFERRED_SIZE).addContainerGap(
                            56, Short.MAX_VALUE)));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());

        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(
                general_JPanel, javax.swing.GroupLayout.Alignment.TRAILING,
                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                Short.MAX_VALUE));
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(
                general_JPanel, javax.swing.GroupLayout.DEFAULT_SIZE,
                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));
        pack();

    }    // </editor-fold>//GEN-END:initComponents

    /**
     * Method description
     *
     * @param evt description
     */
    private void computingSE_JCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {    // GEN-FIRST:event_computingSE_JCheckBoxActionPerformed

        if (computingSE_JCheckBox.isSelected()) {


            // let the user know that convergence criterion will be set to 1.0E-8
            // to allow finer convergence in order to estimate the standard errors.
            // if selected YES do the change
            String msg = "Convergence criterion will be set to 1.0E-8"
                         + " to allow finer convergence in order to estimate the standard errors";

            if (SwingUtil.isYesInYesNoOptionDialog(this, msg)) {
                convEpsilon_JTextField.setText("1.0E-8");
            } else {


                // de-select the computing SE option
                computingSE_JCheckBox.setSelected(false);
            }
        }

    }    // GEN-LAST:event_computingSE_JCheckBoxActionPerformed

    /**
     * Method description
     *
     * @param evt description
     */
    private void formWindowClosing(java.awt.event.WindowEvent evt) {    // GEN-FIRST:event_formWindowClosing


        // before closing check if imputePolicyProb_JRadioButton is chosen
        // probe field must have value > 0 and < 1
        if (imputePolicyProb_JRadioButton.isSelected()) {

            String probStr = imputeProb_JTextField.getText();

            if (probStr.length() == 0) {

                SwingUtil.showErrorDialog(this, "The imputation probability field can`t be empty!");

                return;
            }

            try {

                double prob = Double.parseDouble(probStr);

                if (!((prob > 0.0) && (prob < 1.0))) {

                    SwingUtil.showErrorDialog(
                        this, "The imputation probability must be > 0.0 and <= 1.0 !");

                    return;
                }

                imputationProb = prob;

            } catch (NumberFormatException numberFormatException) {

                SwingUtil.showErrorDialog(
                    this, "The imputation probability field doesn`t have a decimal value!");

                return;
            }

            this.dispose();
        }
    }    // GEN-LAST:event_formWindowClosing

    /**
     * Method description
     *
     * @param evt description
     */
    private void imputing_JRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {    // GEN-FIRST:event_imputing_JRadioButtonActionPerformed


        // imputation strategies will be de/selected according to this.
        if (imputing_JRadioButton.isSelected()) {
            imputePolicyProb_JRadioButton.setSelected(true);
        } else {
            imputePolicyProb_JRadioButton.setSelected(false);
        }
    }    // GEN-LAST:event_imputing_JRadioButtonActionPerformed

    public void setOptions(ComGLHoodIModel inputModel) {

        iteration_JTextField.setText("" + inputModel.getIterationLimit());
        convEpsilon_JTextField.setText("" + inputModel.getConvgEpsilon());

        RFLPImputation rflpImputation = inputModel.getRflpImputation();
        int policyIndex               = rflpImputation.getPolicyIndex();

        if (policyIndex == RFLPImputation.POLICY_REMOVE) {

            removeSpore_JRadioButton.setSelected(true);
            imputing_JRadioButton.setSelected(false);
            imputePolicyProb_JRadioButton.setSelected(false);
        }

        if (policyIndex == RFLPImputation.POLICY_PROB) {

            removeSpore_JRadioButton.setSelected(false);
            imputing_JRadioButton.setSelected(true);
            imputePolicyProb_JRadioButton.setSelected(true);
            imputeProb_JTextField.setText("" + rflpImputation.getImputationProb());
        }


        // optional operations
        computingSE_JCheckBox.setSelected(inputModel.getOptionalOperModel().isComputingSE());
        pValue_JCheckBox.setSelected(inputModel.getOptionalOperModel().isComputingPValue());
    }

    public void getOptions(ComGLHoodIModel inputModel) {

        inputModel.setIterationLimit(Integer.parseInt(iteration_JTextField.getText()));
        inputModel.setConvgEpsilon(Double.parseDouble(convEpsilon_JTextField.getText()));

        RFLPImputation rflpImputation = new RFLPImputation();

        if (imputing_JRadioButton.isSelected()) {

            rflpImputation.setPolicyIndex(RFLPImputation.POLICY_PROB);
            rflpImputation.setImputationProb(Double.parseDouble(imputeProb_JTextField.getText()));
        }

        inputModel.setRflpImputation(rflpImputation);


        // optional operations
        inputModel.getOptionalOperModel().setComputingSE(computingSE_JCheckBox.isSelected());
        inputModel.getOptionalOperModel().setComputingPValue(pValue_JCheckBox.isSelected());
    }


    // End of variables declaration//GEN-END:variables
}
